<?php

namespace App\Providers;

use App\Models\UserIntegralLog;
use App\Models\Coupon;
use App\Models\SinInLog;
use App\Models\User;
use App\Models\CompanyInfo;
use App\Observers\CouponObserver;
use App\Observers\GoodsCategoryObserver;
use App\Observers\SinInLogObserver;
use App\Observers\UserIntegralLogObserve;
use App\Observers\UserObserver;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use App\Models\GoodsCategory;
use Illuminate\Http\Resources\Json\JsonResource;

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        GoodsCategory::observe(GoodsCategoryObserver::class);
        Coupon::observe(CouponObserver::class);
        User::observe(UserObserver::class);
        SinInLog::observe(SinInLogObserver::class);
        UserIntegralLog::observe(UserIntegralLogObserve::class);

        JsonResource::withoutWrapping();

        //共享视图变量
        View::share("companyInfo",CompanyInfo::first());
        \Illuminate\Pagination\Paginator::useBootstrap();

        if (!Collection::hasMacro('paginate')) {
            Collection::macro('paginate',
                function ($perPage=15,$page=null,$options=[]){
                $page = $page?:(Paginator::resolveCurrentPage()?:1);
                return (new LengthAwarePaginator(
                    $this->forPage($page,$perPage),$this->count(),$perPage,$page,$options))
                    ->withPath('');
                });
        }
    }
}
